package com.ruoyi.common.utils;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EryaHelper {

    public static String convert2html(String classify, String detail, String zhushu) {

        StringBuffer sb = new StringBuffer();
        sb.append("<ul>");

        List<Map> list = JsonUtil.toListObject(detail, Map.class);
        if (!CollectionUtils.isEmpty(list)) {
            for (Map map : list) {
                String c = map.get("c").toString();
                String r = map.get("r").toString();
                sb.append(String.format("<li><u>%s</u></li>", c));
                sb.append(String.format("<p class=\"sub\">%s</p>", r));
            }
        }

        sb.append("</ul>");
        sb.append("<hr>");

        sb.append(formatZhushu(zhushu));

        return sb.toString();
    }

    public static String formatZhushu(String zhushu) {
        StringBuffer sb = new StringBuffer();
        int start = zhushu.indexOf("○释曰：");
        sb.append("<ul class='zhushu'>");
        sb.append("<li><u>" + zhushu.substring(0, start) + "</u></li>");
        zhushu = zhushu.substring(start, zhushu.length());
        List<String> sentences = split(zhushu);
        for (String sentence : sentences) {
            if (sentence.startsWith("○注")) {
                sb.append("<li><u class='zhu'>" + sentence + "</u></li>");
            } else {
                sb.append("<li><u>" + sentence + "</u></li>");
            }
        }
        sb.append("</ul>");
        return sb.toString();
    }

    private static List<String> split(String zhushu) {
        List<String> sentences = new ArrayList<>();
        int start = 0;
        int end = -1;
        while (true) {
            int idx = getIdx(zhushu);
            System.out.println(idx);
            if (idx > 0) {
                end = idx;
                String sentence = zhushu.substring(start, end);
                zhushu = zhushu.substring(end, zhushu.length());
                sentences.add(sentence);
            } else {
                sentences.add(zhushu);
                break;
            }
        }
        return sentences;
    }

    private static int getIdx(String zhushu) {
        int idx1 = zhushu.indexOf("○释曰", 3);
        int idx2 = zhushu.indexOf("○注", 3);
        if (idx1 < 0 && idx2 < 0) {
            return -1;
        } else if (idx1 < 0) {
            return idx2;
        } else if (idx2 < 0) {
            return idx1;
        } else {
            if (idx1 < idx2) {
                return idx1;
            } else {
                return idx2;
            }
        }
    }

    public static void main(String[] args) {
        String zhushu = "[疏]“命令”至“告也”。○释曰：皆谓告谕也。命者，使告也。《诗•唐风•扬之水》云：“我闻有命。”令，发号以告也。《论语》云：“其身正，不令而行。”畛者，致告也。祈者，求告也。《书•召诰》云：“祈天永命。”请者，言告也。婚礼五曰请期。谒者，告白也。《月令》曰：“谒於天子。”讯者，告问也。《诗》云：“歌以讯之。”诰者，布告也。《书•大诰》、《洛诰》之类是也。○注“《礼记》曰：畛於鬼神”。○释曰：下《曲礼》文";
        System.out.println(formatZhushu(zhushu));
    }
}


